﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MadPropsDemo.RecipeEditor;
using System.Windows.Forms;

namespace MadPropsDemo.Tests.RecipeEditorTests
{
    [TestClass]
    public class when_the_user_clicks_Save : RecipeEditorTestBase
    {
        [TestMethod]
        public void ok_path()
        {
            // arrange
            RecipeName = "pancakes";
            Assert.IsTrue(HasUnsavedChanges);

            fakeDialogService.ReturnValue = DialogResult.OK;
            fakeDialogService.OnCalled = d =>
            {
                var dialog = (SaveFileDialog)d;

                // the initial filename is the name of the recipe
                Assert.AreEqual("pancakes", dialog.FileName);

                // the filter is correct
                Assert.AreEqual(ExpectedDialogFilter, dialog.Filter);

                // the user sets the file name to something else
                dialog.FileName = "the user entered this";
            };

            // act
            viewModel.Commands.Save.Execute(null);

            // a SaveFileDialog was shown
            Assert.AreEqual(1, fakeDialogService.TimesCalled);

            // the user clicked OK

            // the data was passed to the Recipe Service
            Assert.AreEqual(1, fakeRecipeService.Save_TimesCalled);
            Assert.AreEqual("pancakes", fakeRecipeService.Save_CapturedRecipe.Name);
            Assert.AreEqual("the user entered this", fakeRecipeService.Save_CapturedFilename);

            // the Unsaved Changes flag was cleared
            Assert.IsFalse(HasUnsavedChanges);
        }

        [TestMethod]
        public void cancel_path()
        {
            // arrange
            RecipeName = "spaghetti";
            Assert.IsTrue(HasUnsavedChanges);

            fakeDialogService.ReturnValue = DialogResult.Cancel;

            // act
            viewModel.Commands.Save.Execute(null);

            // a SaveFileDialog was shown
            Assert.AreEqual(1, fakeDialogService.TimesCalled);
            var dialog = (SaveFileDialog)(fakeDialogService.CapturedDialog);

            // the default filename was the name of the recipe
            Assert.AreEqual("spaghetti", dialog.FileName);

            // the user clicked Cancel

            // the recipe service was not called
            Assert.AreEqual(0, fakeRecipeService.Save_TimesCalled);

            // the Unsaved Changes flag remains set
            Assert.IsTrue(HasUnsavedChanges);
        }
    }
}
